package com.molyfun.parents.utils;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.molyfun.parents.fragment.dialog.DatePickerDialogFragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static java.lang.System.currentTimeMillis;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/22/16.
 */
public class TimeUtils {

    //10月8日 20:00:00
    private static final String DEFAULT_DATETIME_PATTERN = "yyyy年MM月dd日 HH:mm:ss";

    private static final String DEFAULT_DATETIME_PATTERN_MINUTE = "yyyy-MM-dd HH:mm";

    private static final String DEFAULT_DATE_PATTERN = "MM月dd日";

    private static final String DEFAULT_YEAR_PATTERN = "yyyy年";

    private static final String DEFAULT_QUERY_DATE_PATTERN = "yyyy-MM-dd";

    private static final String DEFAULT_QUERY_DATE_TILE_MONTH_PATTERN = "yyyy-MM";

    private static final String DEFAULT_TIME_PATTERN = "HH:mm";


    /**
     * 设置弹窗显示日期
     *
     * @param context
     */

    public static void getChooseDate(Context context, final DatePickerDialogFragment.IDateCallback dateCallback) {
        new DatePickerDialogFragment.DatePickerDialogBuilder().setCallback(new DatePickerDialogFragment.IDateCallback() {
            @Override
            public void onTimeChoosed(String time) {
                if (dateCallback != null) {
                    dateCallback.onTimeChoosed(time);
                }
            }
        }).build(((FragmentActivity) context).getSupportFragmentManager().beginTransaction());
    }

    /**
     * 2016-10-08
     *
     * @return
     */
    public static String getPreDate(String dateStr) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_QUERY_DATE_PATTERN);
        if (TextUtils.isEmpty(dateStr)) {
            date = new Date();
        } else {
            try {
                date = sdf.parse(dateStr);
            } catch (ParseException e) {
                date = new Date();
                e.printStackTrace();
            }
        }
        date.setTime(date.getTime() - 86400000l);
        return sdf.format(date);
    }


    public static boolean isAfterToday(String dateStr) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_QUERY_DATE_PATTERN);
        if (TextUtils.isEmpty(dateStr)) {
            date = new Date();
        } else {
            try {
                date = sdf.parse(dateStr);
            } catch (ParseException e) {
                date = new Date();
                e.printStackTrace();
            }
        }
        return (date.getTime() - System.currentTimeMillis()) > 0;
    }

    public static String getNextDate(String dateStr) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_QUERY_DATE_PATTERN);
        if (TextUtils.isEmpty(dateStr)) {
            date = new Date();
        } else {
            try {
                date = sdf.parse(dateStr);
            } catch (ParseException e) {
                date = new Date();
                e.printStackTrace();
            }
        }
        date.setTime(date.getTime() + 86400000l);
        return sdf.format(date);
    }


    /**
     * 时间字符串转Date
     *
     * @param dateStr
     * @return
     */
    public static Date getDateByStr(String dateStr) {
        if (TextUtils.isEmpty(dateStr)) {
            return new Date();
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
            Date date = sdf.parse(dateStr);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 2016-10-08
     *
     * @return
     */
    public static String getQueryDate() {
        Date date = new Date(currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_QUERY_DATE_PATTERN);
        return format.format(date);
    }

    /**
     * 2016-10-08
     *
     * @return
     */
    public static String getCalendarDate(Calendar calendar) {
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_QUERY_DATE_PATTERN);
        return format.format(date);
    }

    /**
     * 2016-10-08
     *
     * @return
     */
    public static String getDateByTimeMillis(long timeMillis) {
        Date date = new Date(timeMillis);
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_QUERY_DATE_PATTERN);
        return format.format(date);
    }

    /**
     * 2016-10-08 HH:mm
     *
     * @return
     */
    public static String getDateMinuteByTimeMillis(long timeMillis) {
        Date date = new Date(timeMillis);
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATETIME_PATTERN_MINUTE);
        return format.format(date);
    }

    /**
     * 2016-10
     *
     * @return
     */
    public static String getDateTileMonthByTimeMillis(long timeMillis) {
        Date date = new Date(timeMillis);
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_QUERY_DATE_TILE_MONTH_PATTERN);
        return format.format(date);
    }

    /**
     * 00:02:23
     *
     * @return
     */
    private static final int HOUR_SECOND = 3600;//60 * 60

    public static String getCountDownByTimeMillis(long timeMillis) {
        int second = (int) ((timeMillis - System.currentTimeMillis()) / 1000);
        if (second < 0) {
            return "00:00:00";
        }
        int hour = second / HOUR_SECOND;
        int minutes = (second - (hour * HOUR_SECOND)) / 60;
        int secondShow = second % 60;
        StringBuilder sb = new StringBuilder();
        if (hour > 9) {
            sb.append(hour + ":");
        } else {
            sb.append("0" + hour + ":");
        }
        if (minutes > 9) {
            sb.append(minutes + ":");
        } else {
            sb.append("0" + minutes + ":");
        }
        if (secondShow > 9) {
            sb.append(secondShow);
        } else {
            sb.append("0" + secondShow);
        }

        return sb.toString();
    }

    public static String getPlayedTimeByTimeMillis(long timeMillis) {
        int second = (int) ((timeMillis) / 1000);
        if (second < 0) {
            return "00:00:00";
        }
        int hour = second / HOUR_SECOND;
        int minutes = (second - (hour * HOUR_SECOND)) / 60;
        int secondShow = second % 60;
        StringBuilder sb = new StringBuilder();
        if (hour > 9) {
            sb.append(hour + ":");
        } else {
            sb.append("0" + hour + ":");
        }
        if (minutes > 9) {
            sb.append(minutes + ":");
        } else {
            sb.append("0" + minutes + ":");
        }
        if (secondShow > 9) {
            sb.append(secondShow);
        } else {
            sb.append("0" + secondShow);
        }

        return sb.toString();
    }


    /**
     * 2017年10月8日 20:00:00
     *
     * @param ms 时间戳
     * @return
     */
    public static String transForDateTime(long ms) {
        Date date = new Date(ms);
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATETIME_PATTERN);
        return format.format(date);
    }

    /**
     * 10月8日
     *
     * @param ms 时间戳
     * @return
     */
    public static String transForDate(long ms) {
        Date date = new Date(ms);
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        return format.format(date);
    }


    /**
     * 10月8日
     *
     * @param ms 时间戳
     * @return
     */
    public static String getYearForDate(long ms) {
        Date date = new Date(ms);
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_YEAR_PATTERN);
        return format.format(date);
    }

    /**
     * 20:00
     *
     * @param ms 时间戳
     * @return
     */
    public static String transForTime(long ms) {
        Date date = new Date(ms);
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_TIME_PATTERN);
        return format.format(date);
    }

    /**
     * long 类型的时间转换为String
     *
     * @param currentTime    long时间
     * @param formatType 时间格式
     * @return String
     */
    public static String longToString(long currentTime, String formatType) {
        Date date = null; // long类型转成Date类型
        try {
            date = longToDate(currentTime, formatType);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String strTime = dateToString(date, formatType); // date类型转成String
        return strTime;
    }


    /**
     * long 类型的时间转换为date
     *
     * @param currentTime    long时间
     * @param formatType 时间格式
     * @return date
     */
    public static Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    /**
     * Date 类型的时间转换为String
     *
     * @param data    时间
     * @param formatType 时间格式
     * @return String
     */
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    /**
     * String 类型的时间转换为Date
     *
     * @param strTime    String时间
     * @param formatType 时间格式
     * @return date
     */
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    /**
     * 根据出生时间戳算年龄年月日
     *
     * @param birthdatetime 出生时间戳
     * @return 年月日
     */
    public static String getAge(long birthdatetime) {


        if (birthdatetime == 0) {
            return "未知年龄";
        }
        long time = currentTimeMillis() - birthdatetime;
        int days = (int) Math.abs(time / 1000 / 60 / 60 / 24);
        int year = days / 365;
        int month = (int) ((days - year * 365) / 30.5);
        int day = (int) (days - year * 365 - month * 30.5);
        return year + "岁" + month + "月" + day + "天";

    }

    public static String getWeekDayName(int dayOfWeek) {
        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                return "星期日";
            case Calendar.MONDAY:
                return "星期一";
            case Calendar.TUESDAY:
                return "星期二";
            case Calendar.WEDNESDAY:
                return "星期三";
            case Calendar.THURSDAY:
                return "星期四";
            case Calendar.FRIDAY:
                return "星期五";
            case Calendar.SATURDAY:
                return "星期六";
            default:
                return "";
        }
    }

}
